<template>
    <div class="tourism-container">
      <!-- 左侧导航栏 -->
      <Sidebar />
  
      <!-- 中间部分：景点展示 -->
      <div class="main-content">

        <div class="search-bar-container">
          <!-- 搜索输入框 -->
          <el-input
            v-model="searchQuery"
            placeholder="搜索景点"
            clearable
            @focus="showDropdown = true"
            @blur="handleBlur"
            @input="handleSearchInput"
            @clear="clearSearch"
            class="search-bar-input"
          >
            <template #prefix>
              <el-icon><Search /></el-icon>
            </template>
          </el-input>

          <!-- 搜索按钮 -->
          <el-button
            icon="Search"
            type="primary"
            class="search-button"
            @click="handleSearchButtonClick"
          >
            搜索
          </el-button>

          <!-- 搜索下拉框 -->
          <div v-if="showDropdown" class="search-dropdown">
            <!-- 搜索历史 -->
            <div class="search-history">
              <h4 class="dropdown-title">搜索历史</h4>
              <ul class="dropdown-list">
                <li
                  v-for="(history, index) in searchHistory"
                  :key="index"
                  @mousedown.prevent="navigateToSpot(history.id)"
                  class="dropdown-item"
                >
                  <el-icon><Clock /></el-icon>
                  {{ history.name }}
                  <el-icon
                    class="delete-icon"
                    @mousedown.prevent="deleteHistory(history.id)"
                  >
                    <Close />
                  </el-icon>
                </li>
              </ul>
            </div>

            <!-- 猜你想搜 -->
            <div class="search-suggestions">
              <h4 class="dropdown-title">猜你想搜</h4>
              <ul class="dropdown-list">
                <li
                  v-for="spot in filteredSpots"
                  :key="spot.id"
                  @mousedown.prevent="navigateToSpot(spot.id)"
                  class="dropdown-item"
                >
                  <el-icon><Location /></el-icon>
                  {{ spot.name }}
                </li>
              </ul>
            </div>
          </div>
        </div>

        <h1 class="title">旅游推荐</h1>
        <div class="card-container">
          <div v-for="(spot, index) in spots" 
          :key="index" 
          class="card"
          @click="handleCardClick(spot)"
          >
            <img :src="spot.image" alt="景点图片" class="card-image" />
            <div class="card-content">
              <h3 class="card-title">{{ spot.name }}</h3>
              <div class="card-actions">
                <el-button
                  type="primary"
                  :icon="spot.isLiked ? StarFilled : Star"
                  @click.stop="toggleLike(spot)"
                >
                  {{ spot.likes }} 喜欢
                </el-button>
                <el-button type="success" @click.stop="handleWriteDiary(spot)">
                  <el-icon><Edit /></el-icon>
                  写日记
                </el-button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import Sidebar from '@/components/Sidebar.vue' // 引入导航栏组件
  import { ref, computed } from 'vue'
  import { Location, Notebook, Star, StarFilled, Edit, SwitchButton ,Clock,Close} from '@element-plus/icons-vue'
  import { useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'

  
  const router = useRouter()
  
  // 模拟景点数据
  const spots = ref([
    {
      id: 1,
      name: '故宫',
      image: 'https://via.placeholder.com/300',
      description: '这是景点1的描述',
      isLiked: false, // 是否喜欢
      likes: 10 // 喜欢总数
    },
    {
      id: 2,
      name: '颐和园',
      image: 'https://via.placeholder.com/300',
      description: '这是景点2的描述',
      isLiked: false,
      likes: 5
    },
    {
      id: 3,
      name: '北京邮电大学',
      image: 'https://via.placeholder.com/300',
      description: '这是景点3的描述',
      isLiked: false,
      likes: 8
    },
    {
      id: 4,
      name: '天坛',
      image: 'https://via.placeholder.com/300',
      description: '这是景点4的描述',
      isLiked: false,
      likes: 8
    },
    {
      id: 5,
      name: '圆明园',
      image: 'https://via.placeholder.com/300',
      description: '这是景点5的描述',
      isLiked: false,
      likes: 8
    },
    {
      id: 6,
      name: '奥林匹克公园',
      image: 'https://via.placeholder.com/300',
      description: '这是景点6的描述',
      isLiked: false,
      likes: 8
    },
    {
      id: 7,
      name: '南锣鼓巷',
      image: 'https://via.placeholder.com/300',
      description: '这是景点7的描述',
      isLiked: false,
      likes: 8
    }
  ])
  
  // 点击卡片跳转到 Map 页面
  const handleCardClick = (spot) => {
    router.push({
      path: '/show-map',
      query: { id: spot.id } // 传递景点 ID
    })
  }
  // 切换喜欢状态
  const toggleLike = (spot) => {
    spot.isLiked = !spot.isLiked
    if (spot.isLiked) {
      spot.likes += 1
    } else {
      spot.likes -= 1
    }
  }
  
  // 写日记按钮点击事件
  const handleWriteDiary = (spot) => {
    console.log('撰写日记:', spot.name)
    // 这里可以跳转到写日记页面
    router.push({
    path: '/create-diary',
    query: { title: spot.id}
  })
  }
  
  // 退出登录
  const handleLogout = () => {
    // 清除登录状态（例如清除本地存储的 token）
    localStorage.removeItem('token') // 假设登录状态存储在 localStorage 中
  
    // 提示退出成功
    ElMessage.success('退出登录成功！')
  
    // 跳转到登录页面
    router.push('/login')
  }

  // 搜索相关状态
  const searchQuery = ref('')
  const searchHistory = ref([])
  const showDropdown = ref(false)

  // 过滤后的景点列表
  const filteredSpots = computed(() => {
    if (!searchQuery.value) return []
    return spots.value.filter((spot) =>
      spot.name.includes(searchQuery.value)
    )
  })

  // 搜索输入处理
  const handleSearchInput = () => {
    // 可以在这里添加额外的逻辑，例如调用后端接口获取推荐结果
  }

  // 清空搜索
  const clearSearch = () => {
    searchQuery.value = ''
  }

  // 点击搜索按钮处理
  const handleSearchButtonClick = () => {
    if (searchQuery.value) {
      const matchedSpot = spots.value.find((spot) =>
        spot.name.includes(searchQuery.value)
      )
      if (matchedSpot) {
        navigateToSpot(matchedSpot.id)
      } else {
        alert('未找到匹配的景点')
      }
    }
  }

  // 跳转到景点详情页面
  const navigateToSpot = (id) => {
    // 添加到搜索历史
    const spot = spots.value.find((s) => s.id === id)
    if (spot && !searchHistory.value.some((h) => h.id === id)) {
      searchHistory.value.unshift(spot)
    }

    // 跳转到 SpotDetail 页面
    router.push({
      name: 'SpotDetail',
      params: { id },
    })

    // 隐藏下拉框
    showDropdown.value = false
  }

  // 清除搜索历史
  const deleteHistory = (id) => {
    searchHistory.value = searchHistory.value.filter((h) => h.id !== id)
  }

  // 处理失去焦点时隐藏下拉框
  const handleBlur = () => {
    setTimeout(() => {
      showDropdown.value = false
    }, 200) // 延迟隐藏以确保点击事件触发
  }
  </script>
  
  <style scoped>
  .tourism-container {
    display: flex;
    height: 100vh;
    width: 100%; /* 确保容器占满页面宽度 */
  }
  
  .sidebar {
    width: 16%; /* 占据页面 1/6 宽度 */
    background-color: #f5f5f5;
    padding: 20px;
    box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);
    position: fixed; /* 固定定位 */
    top: 0;
    left: 0;
    bottom: 0;
  }
  .sidebar-title {
    font-size: 18px;
    margin-bottom: 20px;
  }
  .sidebar-menu {
    list-style: none;
    padding: 0;
  }
  .sidebar-menu li {
    margin-bottom: 10px;
  } 
  .sidebar-link {
    display: flex;
    align-items: center;
    text-decoration: none;
    color: #2c3e50;
    padding: 10px;
    border-radius: 4px;
    transition: background-color 0.3s;
  }
  .sidebar-link:hover {
    background-color: #e0e0e0;
  }
  .sidebar-link .el-icon {
    margin-right: 10px;
  }
  .logout-button {
    width: 100%;
    margin-top: 20px; /* 与上方内容保持距离 */
    background-color: #ff4d4f; /* 红色背景 */
    border: none;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .logout-button:hover {
    background-color: #ff7875; /* 鼠标悬停时的背景色 */
  }
  
  .main-content {
    flex: 1;
    padding: 20px;
    background-color: #fff;
    margin-left: 20%; /* 避免内容被导航栏遮挡 */
    display: flex;
    flex-direction: column; /* 改为列布局 */
    align-items: center; /* 水平居中 */
    width: calc(100% - 16.666%); /* 确保内容宽度占满剩余空间 */
  }
  
  .title {
    margin-bottom: 20px; /* 调整标题与内容的间距 */
   /* align-items: center;*/
    font-size: 30px;
    margin-bottom: 30px;
  }
  
  .card-container {
    display: flex;
    flex-wrap: wrap; /* 允许换行 */
    gap: 20px; /* 卡片之间的间距 */
    justify-content: flex-start; /* 卡片相对容器左对齐 */
    padding-left: 30px; /* 增加容器左侧的空隙 */
  }
  
  .card {
    flex: 1 1 calc(33.333% - 20px); /* 每行最多 3 张卡片，间距为 20px */
    max-width: calc(40% - 20px); /* 每张卡片的最大宽度 */
    min-width: 200px; /* 设置卡片的最小宽度，防止内容溢出 */
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    overflow: hidden;
    transition: transform 0.3s, box-shadow 0.3s;
    padding: 15px; /* 增加内边距 */
  }
  
  .card:hover {
    transform: translateY(-5px);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  }
  
  .card-image {
    width: 100%;
    height: 200px; /* 增加图片高度 */
    object-fit: cover;
    border-radius: 8px; /* 图片圆角 */
  }
  
  .card-content {
    padding: 0px;
  }
  
  .card-title {
    font-size: 20px; /* 增加标题字体大小 */
    margin-bottom: 10px;
  }
  
  .card-actions {
    display: flex;
    justify-content: space-between;
  }

    /* 搜索栏容器 */
  .search-bar-container {
    position: relative;
    max-width: 600px; /* 搜索栏宽度 */
    margin: 0 auto;
    display: flex;
    align-items: center;
    background-color: #f5f5f5;
    border-radius: 25px; /* 圆角 */
    padding: 5px 15px; /* 内边距 */
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); /* 阴影效果 */
  }

  /* 搜索输入框 */
  .search-bar-input {
    flex: 1;
    border: none;
    outline: none;
    background: transparent;
    font-size: 16px;
    padding: 10px;
    color: #333;
  }

  /* 搜索按钮图标 */
  .search-button {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 15px;
    color: #999;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.3s;
  }

  .search-button:hover {
    color: #333;
  }

  /* 搜索下拉框 */
  .search-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    width: 100%;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    z-index: 10;
    padding: 15px;
    margin-top: 10px; /* 与搜索栏的间距 */
  }

  /* 下拉框标题 */
  .dropdown-title {
    font-size: 14px;
    margin-bottom: 10px;
    color: #999;
    font-weight: bold;
  }

  /* 搜索历史和猜你想搜的列表 */
  .dropdown-list {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-wrap: wrap; /* 支持多行显示 */
    gap: 10px; /* 项目之间的间距 */
  }

  /* 搜索历史的标签样式 */
  .dropdown-item {
    background: #f5f5f5;
    border-radius: 15px;
    padding: 5px 15px;
    font-size: 14px;
    color: #333;
    cursor: pointer;
    transition: background 0.3s;
  }

  .dropdown-item:hover {
    background: #e0e0e0;
  }

  /* 删除图标 */
  .delete-icon {
    color: #999;
    cursor: pointer;
    margin-left: 10px;
  }

  .delete-icon:hover {
    color: #ff4d4f;
  }
  </style>